package zzz.action;

import java.io.File;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import zzz.model.Advertising;
import zzz.service.AdverService;

@Controller
@RequestMapping("/admanage")
public class ManageAdAction {

	@Autowired
	private AdverService adverService;
	@RequestMapping("/managead")
	public String getAllAd(Map<String,List> m){
		List<Advertising> advers=adverService.getAllAdver();
		
		m.put("advers", advers);
		return "manager/adManager";
	}
	
	@RequestMapping("/addadpage")
	public String addAdPage(){
		return "manager/adManagerAddAd";
	}
	@RequestMapping("/manageraddad")
	public String addAd(HttpServletRequest request,Advertising adver,Map<String,String> m){
		String fileName = adver.getAdPicFile().getOriginalFilename();  
		adver.setAdPic(fileName);
		String adpicpath="";
		
		if(adver.getAdPicFile()!=null){
			
			if(adver.getAdPicFile().getContentType().equals("image/jpeg")||
					adver.getAdPicFile().getContentType().equals("img/png")){
				String oriFilename=adver.getAdPicFile().getOriginalFilename();
				adpicpath="/img/adver/"+oriFilename;
				adver.setAdPic(adpicpath);
			
				String path=request.getServletContext().getRealPath("/")+adpicpath;
				System.out.println("path:"+path);
				File file=new File(path);
				try {
					if(adver.getAdPicFile()!=null){
						adver.getAdPicFile().transferTo(file);
					}
					int rsl=adverService.newAdver(adver);
					if(rsl!=0){
						m.put("msg","添加广告成功");
					    m.put("herf","managead");
					}else{
						m.put("msg","添加广告失败");
					    m.put("herf","managead");
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();			
					m.put("msg","图片上传失败");
					m.put("herf","managead");
				}
			}
			else{
				m.put("msg","上传图片类型不正确，请上传jpg或png格式图片");
				m.put("herf","managead");
			}		
		}
		
		return "common/msgModal";
	}
	
	@RequestMapping("/deladver")
	public String delAdver(String adverId,Map<String,String> m){
		int result=adverService.delAdver(adverId);
		if(result!=0){
			m.put("msg","删除成功");
		    m.put("herf","managead");
		}else{
			m.put("msg","删除失败");
		    m.put("herf","managead");
		}
		return "common/msgModal";
	}
	@RequestMapping("/updateadpage")
	public String updateAdPage(String adverId,Map<String,Advertising> m){
		Advertising adver=adverService.findAdById(adverId);
		m.put("adver", adver);
		return "manager/adUpdateAd";
	}
	@RequestMapping("/managerupdatead")
	public String updateAd(HttpServletRequest request,Advertising adver,Map<String,String> m){
		System.out.println(adver.toString());
		
		m.put("msg","修改成功");
	    m.put("herf","managead");
	    return "common/msgModal";
	}
	
}
